package com.factory.iamp.dao;

import com.factory.iamp.pojo.User;
import com.factory.iamp.pojo.UserList;
import com.factory.iamp.pojo.UserName;
import com.factory.iamp.pojo.UserRegister;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {
    @Select("select count(uid) from user")
    int Count();

    @Select("select count(uid) from user where ${QueryStr}")
    int QueryCount(@Param("QueryStr") String QueryStr);

    @Select("select count(uid) from user where uid like #{name} or username like #{name} or realname like #{name}")
    int findCount(@Param("name") String name);

    @Select("select count(tid) from `table` where handler = #{uid} and type = 1")
    int ToDoOrderNums(@Param("uid") String uid);

    @Select("select count(tid) from `table` where handler = #{uid} and type = 1 and lid = #{line}")
    int ToDoOrderNumsLine(@Param("uid") String uid, @Param("line") int line);

    @Select("select count(tid) from `table` where creator = #{uid} and type > -1")
    int CreateOrderNums(@Param("uid") String uid);

    @Select("select count(tid) from `table` where creator = #{uid} and type > -1 and lid = #{line}")
    int CreateOrderNumsLine(@Param("uid") String uid, @Param("line") int line);

    @Select("select count(tid) from pass_log where `to` = #{uid}")
    int RelateOrderNums(@Param("uid") String uid);

    @Select("select count(pass_log.tid) from pass_log left join `table` on pass_log.tid = `table`.tid where pass_log.`to` = #{uid} and `table`.lid = #{line}")
    int RelateOrderNumsLine(@Param("uid") String uid, @Param("line") int line);

    @Select("select count(uid) from user where role = #{role}")
    int countByRole(@Param("role") int rid);

    @Select({
            "<script>",
            "select count(uid) from user where uid in (",
            "<foreach collection = 'userList' item = 'item' index = 'index' separator = ','>",
            "#{item}",
            "</foreach>)",
            "</script>"
    })
    int listCount(@Param("userList") List<String> userList);

    @Select("select * from user limit #{offset}, #{limit}")
    @Results({
            @Result(property = "role", column = "role", one = @One(select = "com.factory.iamp.dao.RoleMapper.findById"))
    })
    List<UserList> ListPaged(@Param("offset") int offset, @Param("limit") int limit);

    @Select("select * from user where ${QueryStr} limit #{offset}, #{limit}")
    @Results({
            @Result(property = "role", column = "role", one = @One(select = "com.factory.iamp.dao.RoleMapper.findById"))
    })
    List<UserList> QueryListPaged(
            @Param("offset") int offset, @Param("limit") int limit, @Param("QueryStr") String QueryStr
    );

    @Select("select * from user where uid like #{name} or username like #{name} or realname like #{name} limit #{offset}, #{limit}")
    @Results({
            @Result(property = "role", column = "role", one = @One(select = "com.factory.iamp.dao.RoleMapper.findById"))
    })
    List<UserList> findListPaged(@Param("offset") int offset, @Param("limit") int limit, @Param("name") String name);

    @Select("select * from user where uid = #{uid} limit 1")
    User findById(@Param("uid") String uid);

    @Select("select * from user where uid = #{uid} limit 1")
    UserName findByIdUserName(@Param("uid") String uid);

    @Select("select * from user where username = #{name} limit 1")
    User findByName(@Param("name") String userName);

    @Insert("insert into user(uid, username, password, role, realname) " +
            "values(#{user.uid}, #{user.username}, #{user.password}, #{user.role}, #{user.realname})")
    void addUser(@Param("user") UserRegister user);

    @Update("update user set token = #{token} where uid = #{uid}")
    void renewToken(@Param("uid") String uid, @Param("token") String token);

    @Update("update user set password = #{password}, token = #{token} where uid = #{uid}")
    void resetPassword(@Param("uid") String uid, @Param("password") String password, @Param("token") String token);

    @Update({
            "<script>",
            "update user set role = #{rid} where uid in (",
            "<foreach collection = 'member' item = 'item' index = 'index' separator = ','>",
            "#{item}",
            "</foreach>)",
            "</script>"
    })
    void buildRoleUser(@Param("rid") int rid, @Param("member") List<String> member);

    @Update("update user set avatar = #{avatar} where uid = #{uid}")
    void updateAvatar(@Param("uid") String uid, @Param("avatar") String avatar);

    @Update("update user set sign_img = #{signimg} where uid = #{uid}")
    void updateSignImg(@Param("uid") String uid, @Param("signimg") String signimg);

    @Delete("delete from user where uid = #{uid}")
    void deleteUser(@Param("uid") String uid);
}
